package com.example.Controller;

import com.atguigu.model.system.SysUser;
import com.atguigu.vo.system.LoginVo;
import com.atguigu.vo.system.RouterVo;
import com.example.Result;
import com.example.ResultCodeEnum;
import com.example.Service.SysMenuService;
import com.example.Service.SysUserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.*;

@RequestMapping("/admin/system")
@RestController
public class AccountController {
    @Resource
    private SysUserService sysUserService;
    @Resource
    private SysMenuService sysMenuService;

    @PostMapping("/index/login")
    public Result login(@RequestBody LoginVo loginVo) {
        Map<String, Object> map = new HashMap<>();
        String token = sysUserService.getmap(loginVo);
        map.put("token", token);
        return Result.build(map, ResultCodeEnum.SUCCESS);

    }

    @GetMapping("/index/info")
    public Result info(@RequestHeader("token") String token) {
        SysUser sysUser = sysUserService.getinfo(token);
        Map<String, Object> map = new HashMap<>();
//        map.put("roles","[admin]");
        map.put("name", sysUser.getName());
//        map.put("roles","[admin]");
        Long userId = sysUser.getId();
        List<RouterVo> list = sysMenuService.getUserMenulist(userId);
        List<String> buttonList=sysMenuService.getbuttonList(userId);
        map.put("routers", list);
        map.put("buttons",buttonList);
        return Result.build(map, ResultCodeEnum.SUCCESS);
    }
}
